import { reactive, ref, shallowRef, toRefs } from 'vue'
import dayjs from 'dayjs'
import * as echarts from 'echarts'
import * as ReportsApi from "@/service/api/report-analysis.ts";
import getGeneralTrafficAccidentLineChartOption from './data/line-chart.js'
import getGeneralTrafficAccidentBarChartOption from './data/bar.chart.js'
import { DataLine } from '@element-plus/icons-vue';


const queryform = reactive({
  activeMenu: 'a1',
  type: 1,
  daterange: [dayjs().subtract(1, 'month').format('YYYY-MM-DD 00:00:00'), dayjs().format('YYYY-MM-DD 23:59:59')]
})

const getDateRange = () => {
  const [startTime, endTime] = queryform.daterange || []
  if (!startTime || !endTime) {
    return { startTime: dayjs().subtract(1, 'month').format('YYYY-MM-DD 00:00:00'), endTime: dayjs().format('YYYY-MM-DD 23:59:59') }
  }
  return { startTime: dayjs(startTime).format('YYYY-MM-DD 00:00:00'), endTime: dayjs(endTime).format('YYYY-MM-DD 23:59:59') }
}

export function useReportsQueryForm() {

  async function exportAction() {
    const params = getDateRange()
    await ReportsApi.ExportAccidentFile(params)
  }

  return {
    ...toRefs(queryform),
    exportAction
  }
}

/**
 * @description: 一般程序交通事故起数折线图（小时）
 * @return {*}
 */
export function useGeneralTrafficAccidentInHourLineChart() {

  const generalTrafficLineChartRef = ref(null)
  const generalTrafficAccidentLineChartInstance = shallowRef(null)

  function getGeneralTrafficAccidentLineChartData() {
    const params = getDateRange()
    ReportsApi.GetLineChartNumByHour(params).then((data) => {
      const option = getGeneralTrafficAccidentLineChartOption(data)
      generalTrafficAccidentLineChartInstance.value = echarts.init(generalTrafficLineChartRef.value)
      generalTrafficAccidentLineChartInstance.value.setOption(option)
    })
  }
  return {
    generalTrafficLineChartRef,
    generalTrafficAccidentLineChartInstance,
    getGeneralTrafficAccidentLineChartData
  }
}

/**
 * @description: 一般程序交通事故起数柱状图（月份）
 * @return {*}
 */
export function useGeneralTrafficAccidentInMonthBarChart() {

  const generalTrafficBarChartRef = ref(null)
  const generalTrafficAccidentBarChartInstance = shallowRef(null)

  function getGeneralTrafficAccidentBarChartData() {
    const params = getDateRange()
    ReportsApi.GetBarChartNumByMonth(params).then((data) => {
      const option = getGeneralTrafficAccidentBarChartOption(data)
      generalTrafficAccidentBarChartInstance.value = echarts.init(generalTrafficBarChartRef.value)
      generalTrafficAccidentBarChartInstance.value.setOption(option)
    })
  }
  return {
    generalTrafficBarChartRef,
    generalTrafficAccidentBarChartInstance,
    getGeneralTrafficAccidentBarChartData
  }
}


/**
 * @description: 一般程序交通事故死亡人数折线图（小时）
 * @return {*}
 */
export function useGeneralTrafficAccidentDeathNumInHourLineChart() {

  const generalTrafficDeathNumLineChartRef = ref(null)
  const generalTrafficAccidentDeathNumLineChartInstance = shallowRef(null)

  function getGeneralTrafficAccidentDeathNumLineChartData() {
    const params = getDateRange()
    ReportsApi.GetLineChartDeathNumByHour(params).then((data) => {
      const option = getGeneralTrafficAccidentLineChartOption(data)
      generalTrafficAccidentDeathNumLineChartInstance.value = echarts.init(generalTrafficDeathNumLineChartRef.value)
      generalTrafficAccidentDeathNumLineChartInstance.value.setOption(option)
    })
  }
  return {
    generalTrafficDeathNumLineChartRef,
    generalTrafficAccidentDeathNumLineChartInstance,
    getGeneralTrafficAccidentDeathNumLineChartData
  }
}

/**
 * @description: 一般程序交通事故死亡人数柱状图（月份）
 * @return {*}
 */
export function useGeneralTrafficAccidentDeathNumInMonthBarChart() {

  const generalTrafficDeathNumBarChartRef = ref(null)
  const generalTrafficAccidentDeathNumBarChartInstance = shallowRef(null)

  function getGeneralTrafficAccidentDeathNumBarChartData() {
    const params = getDateRange()
    ReportsApi.GetBarChartDeathNumByMonth(params).then((data) => {
      const option = getGeneralTrafficAccidentBarChartOption(data)
      generalTrafficAccidentDeathNumBarChartInstance.value = echarts.init(generalTrafficDeathNumBarChartRef.value)
      generalTrafficAccidentDeathNumBarChartInstance.value.setOption(option)
    })
  }
  return {
    generalTrafficDeathNumBarChartRef,
    generalTrafficAccidentDeathNumBarChartInstance,
    getGeneralTrafficAccidentDeathNumBarChartData
  }
}

/**
 * @description: 交通事故道路分布
 * @return {*}
 */
export function useAccidentRoads() {
  const accidentRoadsTableData = ref([])

  function getAccidentRoadsTableData() {
    const params = getDateRange()
    ReportsApi.GetAccidentRoadsData(params).then((data) => {
      if (data) {
        accidentRoadsTableData.value = data
      }
    })
  }

  return {
    accidentRoadsTableData,
    getAccidentRoadsTableData
  }
}

/**
 * @description: 交通事故道路分布
 * @return {*}
 */
export function useAccidentFrequentlyOccuredRoads() {
  const accidentFrequentlyOccuredRoadsTableData = ref({ common: [], simple: [] })

  function getAccidentFrequentlyOccuredRoadsData() {
    const params = getDateRange()
    ReportsApi.GetAccidentFrequentlyOccuredRoadsData(params).then((data) => {
      if (data) {
        accidentFrequentlyOccuredRoadsTableData.value = data
      }
    })
  }

  return {
    accidentFrequentlyOccuredRoadsTableData,
    getAccidentFrequentlyOccuredRoadsData
  }
}


/**
 * @description: 一般程序事故认定原因统计表
 * @return {*}
 */
export function useAccidentDetermination() {
  const accidentDeterminationTableData = ref([])

  function getAccidentDeterminationTableData() {

    const params = getDateRange()
    ReportsApi.GetAccidentDeterminationData(params).then((data) => {
      if (data) {
        accidentDeterminationTableData.value = data.map((i => ({ ...i, swrsCountYOY: i.swrsCountYOY + '%', ssrsCountYOY: i.ssrsCountYOY + '%' })))
      }
    })
  }

  return {
    accidentDeterminationTableData,
    getAccidentDeterminationTableData
  }
}

/**
 * @description: 乡镇街道事故统计
 * @return {*}
 */
export function useAccidentTowns() {
  const accidentTownsTableData = ref([])

  function getAccidentTownsTableData() {
    const params = getDateRange()
    ReportsApi.GetAccidentTownsData(params).then((data) => {
      if (data) {
        accidentTownsTableData.value = data.map(i => {
          return {
            ...i,
            dataList: i.dataList.map(j => ({
              ...j,
              sgCountRatio: j.sgCountRatio + '%',
              swrsCountRatio: j.swrsCountRatio + '%',
              ssrsCountRatio: j.ssrsCountRatio + '%'
            }))
          }
        })
      }
    })
  }

  getAccidentTownsTableData()

  return {
    accidentTownsTableData,
    getAccidentTownsTableData
  }
}

/**
 * @description: 一般程序交通事故四项指标统计
 * @return {*}
 */
export function useAccidentFourIndicator() {
  const accidentFourIndicatorTableData = ref([])

  function getAccidentFourIndicatorTableData() {
    const params = getDateRange()
    ReportsApi.GetAccidentFourIndicatorData(params).then((data) => {
      if (data) {
        accidentFourIndicatorTableData.value = data.map(i => {

          return {
            ...i,
            sgCountYOY: i.sgCountYOY + '%',
            swrsCountYOY: i.swrsCountYOY + '%',
            ssrsCountYOY: i.ssrsCountYOY + '%',
            zjccssYOY: i.zjccssYOY ? i.zjccssYOY + '%' : i.zjccssYOY,
          }

        })

      }
    })
  }

  getAccidentFourIndicatorTableData()

  return {
    accidentFourIndicatorTableData,
    getAccidentFourIndicatorTableData
  }
}


/**
 * @description: 事故形态统计表
 * @return {*}
 */
export function useAccidentForms() {
  const accidentFormsTableData = ref([])

  function getAccidentFormsTableData() {
    const params = getDateRange()
    ReportsApi.GetAccidentFormsData(params).then((data) => {
      if (data) {
        accidentFormsTableData.value = data.map((i => {
          return {
            ...i,
            dataList: i.dataList.map(j => ({
              ...j,
              sgCountYOY: j.sgCountYOY + '%',
              sgCountRatio: j.sgCountRatio + '%',

              swrsCountYOY: j.swrsCountYOY + '%',
              swrsCountRatio: j.swrsCountRatio + '%',

              ssrsCountYOY: j.ssrsCountYOY + '%',
              ssrsCountRatio: j.ssrsCountRatio + '%'
            }))
          }
        }))
      }
    })
  }

  return {
    accidentFormsTableData,
    getAccidentFormsTableData
  }
}


/**
 * @description: 隔离设施情况
 * @return {*}
 */
export function useAccidentGlss() {

  const accidentGlssModel = reactive({
    accidentGlssTableData: [],
    accidentZyGlssTableData: [],
    accidentFhGlssTableData: [],
  })

  function getaccidentGlssTableData() {
    const params = getDateRange()
    ReportsApi.GetAccidentGlssData(params).then((data) => {

      if (data) {
        accidentGlssModel.accidentGlssTableData = data.map(i => ({ ...i, sgsRatio: i.sgsRatio + '%', swrsRatio: i.swrsRatio + '%', ssrsRatio: i.ssrsRatio + '%' }))
      }
    })
  }

  function getaccidentZyGlssTableData() {
    const params = getDateRange()
    ReportsApi.GetAccidentZyGlssData(params).then((data) => {

      if (data) {
        accidentGlssModel.accidentZyGlssTableData = data.map(i => ({ ...i, sgsRatio: i.sgsRatio + '%', swrsRatio: i.swrsRatio + '%', ssrsRatio: i.ssrsRatio + '%' }))
      }
    })
  }

  function getaccidentFhGlssTableData() {
    const params = getDateRange()
    ReportsApi.GetAccidentFhGlssData(params).then((data) => {
      if (data) {
        accidentGlssModel.accidentFhGlssTableData = data.map(i => ({ ...i, sgsRatio: i.sgsRatio + '%', swrsRatio: i.swrsRatio + '%', ssrsRatio: i.ssrsRatio + '%' }))
      }
    })
  }

  return {
    ...toRefs(accidentGlssModel),
    getaccidentGlssTableData,
    getaccidentZyGlssTableData,
    getaccidentFhGlssTableData
  }
}

/**
 * @description: 车辆行驶状态
 * @return {*}
 */
export function useVehicleDrivingStatus() {
  const vehicleDrivingStatusTableData = ref([])

  function getDrivingStatusTableData() {
    const params = getDateRange()
    ReportsApi.GetDrivingStatusData(params).then((data) => {
      if (data) {
        vehicleDrivingStatusTableData.value = data.map((i => {
          return {
            ...i,
            sgCountRatio: i.sgCountRatio + '%',
            swrsCountRatio: i.swrsCountRatio + '%',
            ssrsCountRatio: i.ssrsCountRatio + '%',
          }
        }))
      }
    })
  }

  return {
    vehicleDrivingStatusTableData, getDrivingStatusTableData
  }
}

/**
 * @description: 车辆登记、检验情况
 * @return {*}
 */
export function useVehicleInspection() {
  const vehicleInspectionTableData = ref([])

  function getVehicleInspectionTableData() {
    const params = getDateRange()
    ReportsApi.GetVehicleInspectionData(params).then((data) => {
      if (data) {

        vehicleInspectionTableData.value = data.map(i => ({
          ...i,
          sgCountYOY: i.sgCountYOY + '%', swrsCountYOY: i.swrsCountYOY + '%', ssrsCountYOY: i.ssrsCountYOY + '%',
          sgCountRatio: i.sgCountRatio + '%', swrsCountRatio: i.swrsCountRatio + '%', ssrsCountRatio: i.ssrsCountRatio + '%'
        }))
      }
    })
  }


  return {
    vehicleInspectionTableData,
    getVehicleInspectionTableData
  }
}


/**
 * @description: 车辆性质
 * @return {*}
 */
export function useVehicleType() {
  const vehicleTypeTableData = ref([])

  function getVehicleTypeTableData() {
    const params = getDateRange()
    ReportsApi.GetVehicleTypeData(params).then((data) => {
      if (data) {
        vehicleTypeTableData.value = data.map(i => ({
          ...i,
          sgCountYOY: i.sgCountYOY + '%', swrsCountYOY: i.swrsCountYOY + '%', ssrsCountYOY: i.ssrsCountYOY + '%',
          sgCountRatio: i.sgCountRatio + '%', swrsCountRatio: i.swrsCountRatio + '%', ssrsCountRatio: i.ssrsCountRatio + '%'
        }))
      }
    })
  }

  return {
    vehicleTypeTableData,
    getVehicleTypeTableData
  }
}

/**
 * @description: 事故分析 天气
 * @return {*}
 */
export function useAccidentByTq() {
  const envWeatherTableData = ref([])

  function getAccidentByTqData() {
    const params = getDateRange()
    ReportsApi.GetAccidentByTq(params).then((data) => {
      if (data) {
        envWeatherTableData.value = data.map(i => ({ ...i, sgCountRatio: i.sgCountRatio + '%', swrsCountRatio: i.swrsCountRatio + '%', ssrsCountRatio: i.ssrsCountRatio + '%' }))
      }
    })
  }


  return {
    envWeatherTableData,
    getAccidentByTqData
  }
}


/**
 * @description: 事故分析 路面
 * @return {*}
 */
export function useAccidentByLmzk() {
  const envRoadTableData = ref([])

  function getAccidentByLmzkData() {
    const params = getDateRange()
    ReportsApi.GetAccidentByLmzk(params).then((data) => {
      if (data) {
        envRoadTableData.value = data.map(i => ({ ...i, sgCountRatio: i.sgCountRatio + '%', swrsCountRatio: i.swrsCountRatio + '%', ssrsCountRatio: i.ssrsCountRatio + '%' }))
      }
    })
  }


  return {
    envRoadTableData,
    getAccidentByLmzkData
  }
}


/**
 * @description: 事故分析 路表
 * @return {*}
 */
export function useAccidentByLbqk() {
  const envRoadFaceTableData = ref([])

  function getAccidentByLbqkData() {
    const params = getDateRange()
    ReportsApi.GetAccidentByLbqk(params).then((data) => {
      if (data) {
        envRoadFaceTableData.value = data.map(i => ({ ...i, sgCountRatio: i.sgCountRatio + '%', swrsCountRatio: i.swrsCountRatio + '%', ssrsCountRatio: i.ssrsCountRatio + '%' }))
      }
    })
  }

  return {
    envRoadFaceTableData,
    getAccidentByLbqkData
  }
}

/**
 * @description: 事故分析 照明
 * @return {*}
 */
export function useAccidentByZmtj() {
  const envLightingTableData = ref([])

  function getAccidentByZmtjData() {
    const params = getDateRange()
    ReportsApi.GetAccidentByZmtj(params).then((data) => {
      if (data) {
        envLightingTableData.value = data.map(i => ({ ...i, sgCountRatio: i.sgCountRatio + '%', swrsCountRatio: i.swrsCountRatio + '%', ssrsCountRatio: i.ssrsCountRatio + '%' }))
      }
    })
  }


  return {
    envLightingTableData,
    getAccidentByZmtjData
  }
}


/**
 * @description: 交通事故责任者交通方式
 * @return {*}
 */
export function useAccidentDriverTransportFeature() {
  const accidentDriverTransportData = ref([])

  function getAccidentDriverTransportData() {
    const params = getDateRange()
    ReportsApi.GetAccidentDriverTransportData(params).then((data) => {
      if (data) {
        accidentDriverTransportData.value = data.map(i => {
          return {
            ...i,
            sgCountYOY: i.sgCountYOY + '%',
            swrsCountYOY: i.swrsCountYOY + '%',
            ssrsCountYOY: i.ssrsCountYOY + '%',
          }
        })
      }
    })
  }


  return {
    accidentDriverTransportData,
    getAccidentDriverTransportData
  }
}

/**
 * @description: 交通事故责任者驾龄
 * @return {*}
 */
export function useAccidentDriverLicenseAgeFeature() {
  const accidentDriverLicenseAgeData = ref([])

  function getAccidentDriverLisenceAgeData() {
    const params = getDateRange()
    ReportsApi.GetAccidentDriverLisenceAgeData(params).then((data) => {
      if (data) {
        accidentDriverLicenseAgeData.value = data.map(i => {
          return {
            ...i,
            sgCountRatio: i.sgCountRatio + '%',
            swrsCountRatio: i.swrsCountRatio + '%',
            ssrsCountRatio: i.ssrsCountRatio + '%',
          }
        })
      }
    })
  }


  return {
    accidentDriverLicenseAgeData,
    getAccidentDriverLisenceAgeData
  }
}

/**
 * @description: 交通事故责任者年龄
 * @return {*}
 */
export function useAccidentDriverAgeFeature() {
  const accidentDriverAgeData = ref([])

  function getAccidentDriverAgetData() {
    const params = getDateRange()
    ReportsApi.GetAccidentDriverAgetData(params).then((data) => {
      if (data) {
        accidentDriverAgeData.value = data.map(i => {
          return {
            ...i,
            sgCountRatio: i.sgCountRatio + '%',
            swrsCountRatio: i.swrsCountRatio + '%',
            ssrsCountRatio: i.ssrsCountRatio + '%',
          }
        })
      }
    })
  }


  return {
    accidentDriverAgeData,
    getAccidentDriverAgetData
  }
}

/**
 * @description: 交通方式伤亡人数统计
 * @return {*}
 */
export function useAccidentDriverDeathFeature() {
  const accidentDriverDeathData = ref([])

  function getAccidentDeathData() {
    const params = getDateRange()
    ReportsApi.GetAccidentDeathData(params).then((data) => {
      if (data) {
        accidentDriverDeathData.value = data.map(i => {
          return {
            ...i,
            swrsCountYOY: i.swrsCountYOY + '%',
            ssrsCountYOY: i.ssrsCountYOY + '%',
          }
        })
      }
    })
  }


  return {
    accidentDriverDeathData,
    getAccidentDeathData
  }
}

/**
 * @description: 伤亡人员职业统计
 * @return {*}
 */
export function useAccidentJobFeature() {
  const accidentJobData = ref([])

  function getAccidentJobData() {
    const params = getDateRange()
    ReportsApi.GetAccidentJobData(params).then((data) => {
      if (data) {
        accidentJobData.value = data.map(i => {
          return {
            ...i,
            swrsCountYOY: i.swrsCountYOY + '%',
            ssrsCountYOY: i.ssrsCountYOY + '%',
          }
        })
      }
    })
  }


  return {
    accidentJobData,
    getAccidentJobData
  }
}

/**
 * @description: 伤亡人员年龄统计
 * @return {*}
 */
export function useAccidentAgeFeature() {
  const accidentAgeData = ref([])

  function getAccidentAgeData() {
    const params = getDateRange()
    ReportsApi.GetAccidentAgeData(params).then((data) => {
      if (data) {
        accidentAgeData.value = data.map(i => {
          return {
            ...i,
            swrsCountYOY: i.swrsCountYOY + '%',
            ssrsCountYOY: i.ssrsCountYOY + '%',
          }
        })
      }
    })
  }


  return {
    accidentAgeData,
    getAccidentAgeData
  }
}


export function useHistoricalInvestigationRecords() {
  const historicalInvestigationRecords = ref([])

  function getHistoricalInvestigationRecords() {
    const params = getDateRange()
    const dataprop = ref({
      pageSize: 10,
      pageNum: 1,
      startTime: params.startTime,
      endTime: params.endTime,
    });
    ReportsApi.GetHistoricalInvestigationRecords(dataprop).then((data) => {
      if (data) {
        historicalInvestigationRecords.value = data.rows
      }
    })
  }


  return {
    historicalInvestigationRecords,
    getHistoricalInvestigationRecords
  }
}

export function useAuthPersonManagement() {
  const authPersonManagement = ref([])

  function getAuthPersonManagement() {
    const params = getDateRange()
    const dataprop = ref({
      pageNum: 1,
      pageSize: 1,
      xm: "", //姓名
    });
    ReportsApi.PostPolicePageInfo(dataprop).then((data) => {
      if (data) {
        authPersonManagement.value = data.rows
      }
    })
  }
  return {
    authPersonManagement,
    getAuthPersonManagement
  }
}